
/**
 * 随机文件名
 * @returns 文件名
 */
function guid() {
    let now = new Date().getTime();
    let str = `xxxxxxxx-xxxx-${now}-yxxx`;
    return str.replace(/[xy]/g, function(c) {
        var r = Math.random() * 16 | 0;
        var v = c == 'x' ? r : (r&0x3|0x8);
        return v.toString(16)
    })
}



// 这里用来上传富文本框的上传
function uploadEditorFileToOSS(file,success,fail,type=1,systemId=null) {
    if(type==1){
        postAjaxNoFailTip('/api/file/uploadMineOther',file,function (repsonse){
            success&&success(repsonse);
        },null);
    }else {
        postAjaxNoFailTip('/api/file/uploadPfZbOther?systemId='+systemId,file,function (repsonse){
            success&&success(repsonse);
        },null);
    }
  
}


/**
 * 加载富文本框
 */
function initTinymce(tinymce,eleName,type=1,systemId=null){
    var edit = tinymce.render({
        elem: "#"+eleName,
        height: 400,
        toolbar1: 'undo redo | table | insert | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | fullscreen',
        image_advtab: true,
        paste_data_images: true,
        menubar: false,//禁用标题栏
        automatic_uploads: true,
        media_live_embeds: true,//查看上传的视频
        //图片选择上传
        images_upload_handler: function (blobInfo, success, failure) {
            var xhr, formData;
            var file = blobInfo.blob();//转化为易于理解的file对象
            var isLt10M = file.size / 1024  / 1024 < 10;
            if(!isLt10M){
                failure('上传图片大小不能超过10MB哦!');
                return;
            }
            uploadEditorFileToOSS(file,function(data){
                //上传成功后，将图片地址返回到tinymce
                console.log(data,'****');
                success(data.data);
            },function(error){
                notice.error("上传图片失败");
            },type,systemId);
        },
        file_picker_types: 'file image media',
        file_picker_callback: function (callback, value, meta) {
            //文件分类
            var filetype='.pdf, .txt, .zip, .rar, .7z, .doc, .docx, .xls, .xlsx, .ppt, .pptx, .mp3, .mp4';
            //为不同插件指定文件类型及后端地址
            switch(meta.filetype){
                case 'image':
                    filetype='.jpg, .jpeg, .png, .gif';
                    break;
                case 'media':
                    filetype='.mp3, .mp4';
                    break;
                case 'file':
                default:
            }
            //模拟出一个input用于添加本地文件
            var input = document.createElement('input');
            input.setAttribute('type', 'file');
            input.setAttribute('accept', filetype);
            input.click();
            input.onchange = function() {
                var file = this.files[0];
                uploadEditorFileToOSS(file,function(data){
                        callback(data.data);
                    },function(error){
                        notice.error("上传文件失败");
                },type,systemId);
            };
        }
    });
    return edit;
}